package com.kjj;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.net.Uri;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.SimpleAdapter;

public class PostView {

    private KjjActivity mActivity;
    private ProductList mList;
    private SimpleAdapter productAdapter;
    private ListView productView;
    private ImageButton[] pictureButton = new ImageButton[3];
    private Bitmap[] bitmap = new Bitmap[3];
    private String[] url = new String[3];
    private int currentIndex = 0;
    private int currentPos = 0;

    public PostView(KjjActivity activity, ProductList list) {
	mActivity = activity;
	mList = list;
	init();
    }

    public boolean init() {
	return true;
    }

    public void resetView() {
	pictureButton[0].setImageBitmap(null);
	pictureButton[1].setImageBitmap(null);
	pictureButton[2].setImageBitmap(null);
	bitmap = new Bitmap[3];
	url = new String[3];
	((EditText) mActivity.findViewById(R.id.postPriceEditText)).setText("");
	((EditText) mActivity.findViewById(R.id.postDescriptionEditText))
		.setText("");

    }

    public boolean startView() {
	mActivity.setContentView(R.layout.post);
	productView = (ListView) mActivity.findViewById(R.id.postListView);
	adjustListHeight();
	Button optionButton = (Button) mActivity
		.findViewById(R.id.postOptionButton);
	optionButton.setOnClickListener(new Button.OnClickListener() {

	    @Override
	    public void onClick(View v) {
		mActivity.switchView(KjjActivity.OPTION_VIEW);
	    }

	});
	Button postButton = (Button) mActivity.findViewById(R.id.postAdButton);
	postButton.setOnClickListener(new Button.OnClickListener() {

	    @Override
	    public void onClick(View v) {
		if (bitmap[0] == null && bitmap[1] == null && bitmap[2] == null) {
		    System.out.println("No Picture!!!");
		    return;
		}
		String price = ((EditText) mActivity
			.findViewById(R.id.postPriceEditText)).getText()
			.toString();
		String desc = ((EditText) mActivity
			.findViewById(R.id.postDescriptionEditText)).getText()
			.toString();
		mList.addItem(bitmap, url, price, desc);
		adjustListHeight();
		productAdapter.notifyDataSetChanged();
		resetView();
		productView.requestFocus();
		scrollToAdded();
	    }

	});
	pictureButton[0] = (ImageButton) mActivity
		.findViewById(R.id.postPicButton1);
	pictureButton[1] = (ImageButton) mActivity
		.findViewById(R.id.postPicButton2);
	pictureButton[2] = (ImageButton) mActivity
		.findViewById(R.id.postPicButton3);
	OnClickListener listener = new OnClickListener() {

	    @Override
	    public void onClick(View v) {
		currentIndex = getImageIndex(v);
		mActivity.requestActivity(KjjActivity.REQUEST_CAPTURE, null);
	    }
	};
	for (int i = 0; i < pictureButton.length; i++) {
	    pictureButton[i].setOnClickListener(listener);
	}
	productAdapter = new SimpleAdapter(mActivity, mList.getTable(),
		R.layout.product, new String[] { "image", "price", "desc" },
		new int[] { R.id.productImageView, R.id.productPriceTextView,
			R.id.productDescriptionTextView });
	productAdapter.setViewBinder(new SimpleAdapter.ViewBinder() {

	    @Override
	    public boolean setViewValue(View view, Object data,
		    String textRepresentation) {
		if (view.getId() == R.id.productImageView) {
		    try {
			// Bitmap bmp = Bitmap.createScaledBitmap((Bitmap) data,
			// 50, 50, true);
			((ImageView) view).setImageBitmap((Bitmap) data);
			return true;
		    } catch (Exception e) {
			// e.printStackTrace();
			// System.out.println("not a bitmap!");
			return false;
		    }
		} else {
		    return false;
		}
	    }
	});
	productView.setAdapter(productAdapter);
	productView.setOnItemClickListener(new OnItemClickListener() {
	    @Override
	    public void onItemClick(AdapterView<?> parent, View view,
		    int position, long id) {
		Uri uri = Uri.parse(mList.getProduct(position).link);
		mActivity.requestActivity(KjjActivity.REQUEST_BROWSER, uri);
	    }
	});
	productView.setOnItemLongClickListener(new OnItemLongClickListener() {

	    @Override
	    public boolean onItemLongClick(AdapterView<?> parent, View view,
		    int position, long id) {
		currentPos = position;
		DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {

		    @Override
		    public void onClick(DialogInterface dialog, int which) {
			switch (which) {
			case 0:
			    Uri uri = Uri.parse(mList.getProduct(currentPos).link);
			    mActivity.requestActivity(
				    KjjActivity.REQUEST_BROWSER, uri);
			    break;
			case 1:
			    mList.deleteItem(currentPos);
			    adjustListHeight();
			    productAdapter.notifyDataSetChanged();
			    break;
			}
		    }
		};
		String[] menu = { "查看", "删除" };
		new AlertDialog.Builder(mActivity).setTitle("请选择你要进行的操作")
			.setItems(menu, listener).show();
		return true;
	    }
	});
	return true;
    }

    private int getImageIndex(View view) {
	if (view.getId() == R.id.postPicButton1) {
	    return 0;
	} else if (view.getId() == R.id.postPicButton2) {
	    return 1;
	} else {
	    return 2;
	}
    }

    public boolean addImage(Bitmap bmp) {
	// Drawable[] array = new Drawable[2];
	// array[0] = new BitmapDrawable(bmp);
	// array[1] = new BitmapDrawable(BitmapFactory.decodeResource(
	// mActivity.getResources(), R.drawable.post_pic_mask));
	// LayerDrawable la = new LayerDrawable(array);
	// pictureButton[currentIndex].setImageDrawable(new
	// LayerDrawable(array));
	pictureButton[currentIndex].setImageBitmap(bmp);
	bitmap[currentIndex] = bmp;
	url[currentIndex] = Communication.uploadPicture(bmp);
	return true;
    }

    public void adjustListHeight() {
	ViewGroup.LayoutParams la = productView.getLayoutParams();
	la.height = (Const.PICTURE_SIZE + 1) * mList.getSize();
    }

    public void scrollToAdded() {
	ScrollView sv = (ScrollView) mActivity
		.findViewById(R.id.postScrollView);
	sv.scrollTo(0, 540);
    }
}
